﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;

namespace carShop.Web.Helpers
{
    public class OkWithImage<T> : OkNegotiatedContentResult<T>
    {
        private MemoryStream Ms { get; set; }
        

        public OkWithImage(T content, ApiController controller, MemoryStream ms)
            : base(content, controller)
        {
            Ms = ms;
        }

        public OkWithImage(T content,
                           IContentNegotiator contentNegotiator,
                           HttpRequestMessage request,
                           IEnumerable<MediaTypeFormatter> formatters,
                           MemoryStream ms)
            : base(content, contentNegotiator, request, formatters)
        {
            Ms = ms;
        }


        public override async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response = await base.ExecuteAsync(cancellationToken);

            response.Content = new StreamContent(Ms);
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");

            return response;
        }
    }

}